import flixel.ui.FlxButton;
import flixel.FlxG;
import flixel.util.FlxPath;
import flixel.util.FlxSave;
import flixel.FlxSprite;
import flixel.FlxObject;
import flixel.FlxState;
import flixel.FlxCamera;
import flixel.FlxSubState;
import flixel.group.FlxGroup;
import flixel.group.FlxTypedGroup;
import flixel.FlxBasic;
import flixel.text.FlxText;
import flixel.tile.FlxTilemap;
import haxe.xml.Fast;
import flixel.util.FlxPoint;
import utils.Collision;
import items.Diskette;
import level.Level;
import level.EntryPoint;
import sensors.TouchSensor;

class PlayState extends FlxState
{
	var thelevel: Level;
	
	public var receivesInput: Bool;


	private var FADETIME: Float = 0.3;
	private var entrypoints: Array<EntryPoint>;

	override public function create():Void
	{
		super.create();
		FlxG.log.redirectTraces = true;
		thelevel = new Level(Reg.levelName);
		receivesInput = true;
		FlxG.cameras.bgColor = 0xff333333;
		FlxG.mouse.visible = false;

		add(thelevel);
		FlxG.camera.fade(0xff000000, FADETIME, true);
	}


	
	override public function destroy():Void
	{
		thelevel = null;
		super.destroy();
	}

	/// TODO: que va en el update de acá y qué va en el update de level???
	override public function update(): Void {
		super.update();
		if(FlxG.keys.justPressed.DELETE)
		{
			Reg.slotWipeData();
			FlxG.cameras.fade(0xff000000, FADETIME * 3, false, onMapChange);
			thelevel.character.die();
		}

		FlxG.overlap(thelevel.character, thelevel.overlapableEntities, onOverlapCharacterLevel);
	}

	/**
	 * Función que es llamada cuando el jugador solapa con alguna entidad
	 */
	private function onOverlapCharacterLevel(Character: FlxBasic, Overlapable: FlxBasic)
	{
		var className = Type.getClassName(Type.getClass(Overlapable));
		switch(className){
			case "items.Diskette":
				var diskette = cast(Overlapable, Diskette);
				Reg.setItem("diskette_"+diskette.id);
				diskette.kill();

			case "level.Computer":
				var computer = cast(Overlapable, level.Computer);
				thelevel.showComputerScreen(computer);
				//currentActionContainer = computer.actionContainer;
				//if(FlxG.keys.justPressed.T && !thelevel.findRobotSpawnerById(computer.id).hasAliveRobot()){
				//	openSubState(new CodeState(currentActionContainer, computer.actionSource));
				//	receivesInput = false;
				//}

			case "MessageRegion":
				var messageregion = cast(Overlapable, MessageRegion);
				//messageBoard.showMessage(messageregion.message, 5);
				// evita que vuelva a colisionar con la region del mensaje...
				messageregion.show();

			case "level.ExitRegion":
				var exitRegion = cast(Overlapable, level.ExitRegion);
				Reg.levelName = exitRegion.foreignMap;
				Reg.entryPointId = exitRegion.foreignMapEntryPointId;
				FlxG.cameras.fade(0xff000000, FADETIME, false, onMapChange);

			case "level.Spikes":
				FlxG.cameras.fade(0xff000000, FADETIME * 3, false, onMapChange);
				thelevel.character.die();

			case "sensors.TouchSensor":
				var touchsensor = cast(Overlapable, TouchSensor);
				touchsensor.activate();

			case "level.CannonBullet":
				FlxG.cameras.fade(0xff000000, FADETIME * 3, false, onMapChange);
				thelevel.character.die();

			default:
				throw("UnHandled overlap width: "+className);
		}
	}

	public function onMapChange(){
		FlxG.switchState(new PlayState());
		Reg.save();
	}

/*
	private function onOverlapCharacterComputer(_character: FlxBasic, _computer: FlxBasic){
		var computer = cast(_computer, Computer);
		currentActionContainer = computer.actionContainer;
		if(FlxG.keys.justPressed.T && !findRobotSpawnerById(computer.id).hasAliveRobot()){
			openSubState(new CodeState(currentActionContainer, computer.actionSource));
			
			receivesInput = false;
		}
	}

	private function onOverlapCharacterItem(Character: FlxBasic, Item: FlxBasic){
		//trace(Type.getClassName(Type.getClass(Item)));
		switch(Type.getClassName(Type.getClass(Item)))
		{
			case "items.Diskette":
				var diskette = cast(Item, Diskette);
				diskette.kill();
		}
	}


	private function onOverlapRobotAreaSensors(_robot: FlxBasic, _areaSensor: FlxBasic){
		// preguntar si el robot termino su movimiento acá...
		
		var robot = cast(_robot, Robot);
		var areaSensor = cast(_areaSensor, AreaSensor); 
		
		/// chequeo extra porque FlxG.overlap devuelve cualquier cosa
		if(Collision.myoverlap(robot, areaSensor))
		{
			areaSensor.activate();
		}
		
		//var door = findDoorById(areaSensor.doorId);
		//trace("Robot=>[ " + robot.x +", "+robot.y+", "+robot.width+", "+robot.height+"]");
		//trace("Sensor=>[ " + areaSensor.x +", "+areaSensor.y+", "+areaSensor.width+", "+areaSensor.height+"]");
		//door.open();

	}*/

/*
	public function beginDeadSecuence(){
		FlxG.camera.follow(null);
		character.allowCollisions = 0;
		character.velocity.x = character.velocity.y = 0;
		character.angularVelocity = 60;
		FlxG.cameras.fade(0xff000000, 1, false, endDeadSecuence);
	}

	public function endDeadSecuence(){
		Reg.lastSave.copyTo(Reg.currentState);
		FlxG.resetState();
	}


	public function onOverlapExitRegion(_character: FlxBasic, _exitregion: FlxBasic){
		var exitregion = cast(_exitregion, EntryPoint.ExitRegion);
		Reg.currentState.levelName = exitregion.foreignMap;
		Reg.currentState.entryPointId = exitregion.foreignMapEntryPointId;
		FlxG.cameras.fade(0xff000000, FADETIME, false, onMapChange);
	}

	public function onMapChange(){
		FlxG.switchState(new InGameState());
	}

	public function onOverlapMessageRegion(_character: FlxBasic, _messageregion: FlxBasic){
		var messageregion = cast(_messageregion, MessageRegion);
		messageBoard.showMessage(messageregion.message, 5);
		// evita que vuelva a colisionar con la region del mensaje...
		messageregion.solid = false;
	}
	*/

}
